const nutJs = require('@nut-tree/nut-js');
const capture = require('screenshot-desktop');
const fs = require('fs').promises;
const sharp = require('sharp');

const { mouse } = nutJs;

const screenRate=1.5;//当前系统设置的缩放比例（比如150%，即设置为1.5以精确匹配屏幕尺寸）

async function start() {
    console.log('脚本已经启动');
    try {

        // 1. 截取屏幕
        const tempScreenshot = 'temp-screen.png';
        console.log(`正在截取屏幕，保存到：${tempScreenshot}`);
        const imageBuffer = await capture();
        await fs.writeFile(tempScreenshot, imageBuffer);
        console.log('截图成功');

        // 2. 获取屏幕尺寸并计算中心（精确到整数）
        console.log('正在读取截图尺寸...');
        const metadata = await sharp(tempScreenshot).metadata();
        const screenWidth = metadata.width/screenRate;
        const screenHeight = metadata.height/screenRate;
        
        const centerX = Math.round(screenWidth / 2);
        const centerY = Math.round(screenHeight / 2);
        
        console.log(`✅ 屏幕大小：${screenWidth}x${screenHeight}`);
        console.log(`✅ 计算的中心坐标：(${centerX}, ${centerY})`);

        // 3. 等待2秒（让你有时间观察鼠标初始位置）
        console.log('等待2秒...');
        await new Promise((resolve,reject)=>{
            setTimeout(resolve,2000)
        });

        // 4. 移动鼠标到精确中心
        console.log('测试：正在移动鼠标到中心...');
        await mouse.move({ x: centerX, y: centerY });
        console.log(`✅ 鼠标已移动到中心 (${centerX}, ${centerY})`);


        await fs.unlink(tempScreenshot);
        console.log('✅ 临时文件已清理');

    } catch (err) {
        console.error('\n❌ 脚本执行失败：');
        console.error('错误信息：', err.message);
    }
}

start();
